﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kokomo.Mirror
{
	/// <summary>
	/// Marks a property as a serialized array.
	/// </summary>
	/// <remarks>
	/// The array may have a fixed or variable length.
	/// For a variable-length array, the length may be encoded
	/// within the stream or as a serialized field.
	/// </remarks>
	[AttributeUsage(AttributeTargets.Property)]
	class SerializedArrayAttribute : Attribute
	{
		/// <summary>
		/// Specifies how the length is encoded.
		/// </summary>
		public ArrayLengthEncoding LengthEncoding { get; private set; }
		/// <summary>
		/// The name of the field that specifies the length of the array.
		/// </summary>
		public string LengthFieldName { get; private set; }
		/// <summary>
		/// The fixed length of the array.
		/// </summary>
		public int FixedLength { get; private set; }

		public SerializedArrayAttribute(
			ArrayLengthEncoding lengthEncoding
			)
		{
			this.LengthEncoding = lengthEncoding;
		}
		public SerializedArrayAttribute(
			ArrayLengthEncoding lengthEncoding,
			string lengthFieldName
			)
		{
			if (lengthFieldName == null) throw new ArgumentNullException("lengthFieldName");

			if (
				(lengthEncoding != ArrayLengthEncoding.ElementPrefixField)
				&& (lengthEncoding != ArrayLengthEncoding.BytePrefixField)
				)
			{
				throw new ArgumentException("Incorrect length encoding");
			}

			this.LengthEncoding = lengthEncoding;
			this.LengthFieldName = lengthFieldName;
		}
		public SerializedArrayAttribute(
			ArrayLengthEncoding lengthEncoding,
			int fixedLength
			)
		{
			if (lengthEncoding != ArrayLengthEncoding.Fixed)
			{
				throw new ArgumentException("Incorrect length encoding");
			}

			this.LengthEncoding = lengthEncoding;
			this.FixedLength = fixedLength;
		}
	}
}
